package com.book.service.impl;

import com.book.dao.UserDao;
import com.book.dto.ShopCarDto;
import com.book.dto.UserDto;
import com.book.helper.UserHelper;
import com.book.pojo.User;
import com.book.redis.RedisRandomProvider;
import com.book.service.ShopCarService;
import com.book.service.UserService;
import com.book.util.Constants;
import com.book.util.MD5Utils;
import com.book.util.RedisUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author LiJinCheng
 * @Date 2020/11/12 11:23 下午
 * @Version 1.0
 * @package com.book.service.impl
 **/
@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedisRandomProvider redisRandomProvider;
    @Autowired
    private ShopCarService shopCarService;

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public List<User> getUserList() {
        return userDao.getUserList();
    }

    @Override
    public Integer registerUser(UserDto user) {
        try {
            User user1 = UserHelper.convertDtoToPo(user);
            user1.setUPass(MD5Utils.MD5Encode(user1.getUPass(), "utf-8"));
            user1.setUCode(redisRandomProvider.getCode());
            user1.setUStatus((short) 1);
            user1.setUCreateTime(new Date());
            int i = userDao.insertSelective(user1);
            if (user.getUPhone() != null) {
                redisUtil.sAdd(Constants.USER_PHONE, user.getUPhone());
            }
            if (user.getUName() != null) {
                redisUtil.sAdd(Constants.USER_NAME, user.getUName());
            }
            if (user.getUEmail() != null) {
                redisUtil.sAdd(Constants.USER_EMAIL, user.getUEmail());
            }
            String filePath = Constants.PATH;
            filePath = filePath + "/" + user1.getUCode();
            File folder = new File(filePath);
            if (!folder.exists()) {
                boolean mkdir = folder.mkdir();
            }
            User user2 = userDao.getUserByCode(user1.getUCode());
            ShopCarDto shopCar = new ShopCarDto();
            shopCar.setScCode(user2.getUCode());
            shopCar.setUId(user2.getId());
            shopCarService.insertShopCar(shopCar);
            return i;
        } catch (Exception e) {
            logger.info("用户注册失败", e);
            return 0;
        }
    }

    @Override
    public User getUserByName(UserDto user) {
        User userPo = UserHelper.convertDtoToPo(user);
        return userDao.getUserByParams(userPo);
    }

    @Override
    public User getUserByPhone(UserDto user) {
        User userPo = UserHelper.convertDtoToPo(user);
        return userDao.getUserByParams(userPo);

    }

    @Override
    public UserDto doLogin(UserDto user) {
        try {
            User user1 = UserHelper.convertDtoToPo(user);
            User userByLogin = userDao.getUserByLogin(user1);
            return UserHelper.convertPoToDto(userByLogin);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public Integer updateUser(User user) {
        try {
            return userDao.updateByPrimaryKeySelective(user);
        } catch (Exception e) {
            return 0;
        }
    }

    @Override
    public User getUserByEmail(UserDto userDto) {
        User userPo = UserHelper.convertDtoToPo(userDto);
        return userDao.getUserByParams(userPo);
    }

    @Override
    public UserDto getUserById(Integer uId) {
        User user = userDao.selectByPrimaryKey(uId);
        return UserHelper.convertPoToDto(user);
    }

    @Override
    public Integer getCountByName(String name) {
        return userDao.getCountByName(name);
    }

    @Override
    public Integer getCountByPhone(String phone) {
        return userDao.getCountByPhone(phone);
    }

    @Override
    public Integer getCountByEmail(String email) {
        return userDao.getCountByEmail(email);
    }

    @Override
    public List<String> getUserListString(Integer code) {
       return code == 1?userDao.getUserListStringPhone():code==2?userDao.getUserListStringName():code==3?userDao.getUserListStringEmail():Lists.newArrayList();
    }

    @Override
    public Boolean updatePassword(String pass, String userCode) {
        return userDao.updatePassword(pass,userCode) == 1;
    }

    @Override
    public User getUserByCode(String uCode) {
        return userDao.getUserByCode(uCode);
    }

    @Override
    public List<UserDto> getUserName(List<Integer> ids) {
        List<User> userList = userDao.getUserName(ids);
        if (CollectionUtils.isEmpty(userList)){
            return Lists.newArrayList();
        }
        return userList.stream().map(UserHelper::convertPoToDto).collect(Collectors.toList());
    }
}
